package com.chendx.store.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.user.pojo.User;
import com.chendx.model.user.pojo.UserSign;
import com.chendx.store.user.mapper.UserSignMapper;
import com.chendx.store.user.service.UserSignService;
import com.chendx.store.utils.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by chendx on 2022/4/27.
 */
@Service
public class UserSignServiceImpl extends ServiceImpl<UserSignMapper,UserSign> implements UserSignService {

    @Autowired
    private UserSignMapper userSignMapper;


    @Override
    public ResponseResult sgin() {
        User user = AppThreadLocalUtils.get();
        if(user == null || StringUtils.isEmpty(user.getId()) || StringUtils.isEmpty(user.getUsername())){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        UserSign signInfo =
                getOne(Wrappers.<UserSign>lambdaQuery().eq(UserSign::getUsername, username).eq(UserSign::getUserId, user.getId()));
        if (signInfo == null) {
            signInfo = new UserSign();
            signInfo.setUserId(user.getId());
            signInfo.setUsername(username);
            signInfo.setSignDate(new Date());
            signInfo.setSignCount(1);
            signInfo.setContinuousCount(1);
            signInfo.setSignScore((long) 1);
            save(signInfo);
        }else{
            if (! signDateIsToday(signInfo.getSignDate())) {
                int days = (int) ((new Date().getTime() - signInfo.getSignDate().getTime()) / (1000 * 3600 * 24));
                if (days > 1) {
                    signInfo.setContinuousCount(1);
                    signInfo.setSignScore(signInfo.getSignScore() + 1);
                } else {
                    signInfo.setContinuousCount(signInfo.getContinuousCount() + 1);
                    Long signScore = signInfo.getSignScore();
                    if (signInfo.getContinuousCount() >= 3) {
                        signInfo.setSignScore(signScore + 3);
                    }
                    if (signInfo.getContinuousCount() >= 7) {
                        signInfo.setSignScore(signScore + 5);
                    }
                    if (signInfo.getContinuousCount() >= 15) {
                        signInfo.setSignScore(signScore + 7);
                    }
                    if (signInfo.getContinuousCount() >= 30) {
                        signInfo.setSignScore(signScore + 10);
                    }
                }
                signInfo.setSignDate(new Date());
                signInfo.setSignCount(signInfo.getSignCount() + 1);
                update(signInfo,
                        Wrappers.<UserSign>lambdaQuery()
                                .eq(UserSign::getUsername, username).eq(UserSign::getUserId, user.getId()));
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.TODAY_SIGNED);
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult decrScore() {
        User user = AppThreadLocalUtils.get();
        if(user == null || StringUtils.isEmpty(user.getId()) || StringUtils.isEmpty(user.getUsername())){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        Integer count = userSignMapper.decrScore(username);
        if (count > 0){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SOCRE_ENOUGH);
    }

    @Override
    public ResponseResult addScore(Integer score) {
        User user = AppThreadLocalUtils.get();
        if(user == null || StringUtils.isEmpty(user.getId()) || StringUtils.isEmpty(user.getUsername())){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        try {
            userSignMapper.addScore(username,score);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.okResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult signInfo() {
        User user = AppThreadLocalUtils.get();
        if(user == null || StringUtils.isEmpty(user.getId()) || StringUtils.isEmpty(user.getUsername())){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        UserSign signInfo =
                getOne(Wrappers.<UserSign>lambdaQuery().eq(UserSign::getUsername, username).eq(UserSign::getUserId, user.getId()));
        return ResponseResult.okResult(signInfo);
    }


    public boolean signDateIsToday(Date signDate){
        //判断签到日期是否在今天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newDateStr = sdf.format(new Date());
        String signDateStr = sdf.format(signDate);
        if (newDateStr.equals(signDateStr)){
            return true;
        }else{
            return false;
        }
    }
}
